package com.lujianfei.ann.widget;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

import com.lujianfei.ann.bean.DataPoint;

public class ChartDrawPanel extends JPanel {

	DataPoint[] points;
	int maxX;
	int maxY;
	int minX;
	int minY;

	Line2D.Double line;

	public ChartDrawPanel() {
		super();
		this.setBackground(new Color(0x444444));
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (null == this.points) {
			return;
		}

		// 坐标绘制
		int coordinateW = maxX - minX;
		int coordinateH = maxY - minY;

		// 画 x,y 轴
		g.setColor(new Color(0xffffff));
		int leftX = (getWidth() - coordinateW) / 2;
		int rightX = leftX + coordinateW;
		int topY = (getHeight() - coordinateH) / 2;
		int bottomY = topY + coordinateH;
		g.drawLine(leftX, bottomY, rightX, bottomY);
		g.drawLine(leftX, topY, leftX, bottomY);

		for (int i = 0; i < points.length; i++) {
			DataPoint point = points[i];
			g.setColor(point.getColor());
			g.fillOval(getX(point.x), getY(point.y), 3, 3);
		}

		// 画线
		if (this.line != null) {
			g.setColor(new Color(0x0000ff));
			g.drawLine(getX((int) line.x1), getY((int) line.y1), getX((int) line.x2), getY((int) line.y2));
		}
	}

	public void setLine(Line2D.Double line) {
		this.line = line;
	}

	private int getY(int y) {
		int coordinateH = maxY - minY;

		int topY = (getHeight() - coordinateH) / 2;
		int bottomY = topY + coordinateH;
		return bottomY - y;
	}

	private int getX(int x) {
		int coordinateW = maxX - minX;
		int leftX = (getWidth() - coordinateW) / 2;
		return leftX + x;
	}

	public void setPoints(DataPoint[] points) {
		this.points = points;
	}

	public DataPoint[] getPoints() {
		return points;
	}
	
	public int[] getSetX() {
		int[] results = new int[points.length];
		for (int i = 0; i < results.length; i++) {
			results[i] = points[i].x;
		}
		return results;
	}
	
	public int[] getSetY() {
		int[] results = new int[points.length];
		for (int i = 0; i < results.length; i++) {
			results[i] = points[i].y;
		}
		return results;
	}

	public int getMaxX() {
		return maxX;
	}

	public void setMaxX(int maxX) {
		this.maxX = maxX;
	}

	public int getMaxY() {
		return maxY;
	}

	public void setMaxY(int maxY) {
		this.maxY = maxY;
	}

	public int getMinX() {
		return minX;
	}

	public void setMinX(int minX) {
		this.minX = minX;
	}

	public int getMinY() {
		return minY;
	}

	public void setMinY(int minY) {
		this.minY = minY;
	}

}
